package com.eascs.admin.o2o.settle.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.settle.service.O2oSettleService;
import com.eascs.admin.o2o.settle.vo.O2oSettleVo;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/settle")
public class O2oSettleController extends BaseMasterViewController {

//	@Override
//	public void preHandle() {
//		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "afterLoanManage");
//	}

	@Autowired
	O2oSettleService service;

	@RequestMapping("/index")
	public String index(O2oSettleVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "DCJQ");
		vo.setPageSize(10);
		String o2ourl = "/settle/index";
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();
		String loanUserName = vo.getLoanUserName() == null ? "" : vo.getLoanUserName();
		String dcStartTime = vo.getDcStartTime() == null ? "" : vo.getDcStartTime();
		String dcEndTime = vo.getDcEndTime() == null ? "" : vo.getDcEndTime();
		String cStartTime = vo.getcStartTime() == null ? "" : vo.getcStartTime();
		String cEndTime = vo.getcEndTime() == null ? "" : vo.getcEndTime();
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "dcStartTime", dcStartTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "dcEndTime", dcEndTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "cStartTime", cStartTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "cEndTime", cEndTime);
		o2ourl = o2ourl + "&pageIndex=";

		String titles[] = { "贷款单号", "创建时间", "借款人姓名", "经办平台", "代偿结清时间", "代偿总额(元)", "代偿回款总额(元)", "流程状态" };
		SelectDataOption[] selectDataOptions = { null, null, null, null, null, null, null, new SelectDataOption("O2O_BILLSTATUS") };
		PageResult<Object[]> pageResult = service.getSettlePage(vo);
		Button[] titleButtons = new Button[] { ButtonHelper.createAjaxButton(null, "/rsettle/approl_detail?id=") };
		Button delete = ButtonHelper.createAjaxButton("删除", "/rsettle/delete?eaLoadId=", "DeleteDataSettle");
		delete.setShowButtonFilter(showButtonFilter);
		Button startButton = ButtonHelper.createAjaxButton("发起结清流程", "/rsettle/startUp?eaLoadId=", "updateDataSettle");
		startButton.setShowButtonFilter(showButtonFilter);
		Button[] dealButton = new Button[] { delete, startButton };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setMoreButtons(dealButton);
		o2oPagingInfo.setTitleButtons(titleButtons);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "settle/index";
	}

	ShowButtonFilter showButtonFilter = new ShowButtonFilter() {

		@Override
		public boolean filter(Object obj) {
			Object[] objs = (Object[]) obj;
			if (objs[8].equals(StatusContexts.O2O_NEW)) {
				return true;
			}
			return false;
		}
	};

	public String detail(O2oSettleVo vo) {

		return "settle/detail";
	}
}
